/*
APPENDING DATA ON OCCUPATION

Data 	: 
Folder 	: 
Date	: 

Creator		: Jonas Cederlof (JC)
Description 	: 
*/

********************************************************************************
clear
set more		 off
cap   log close 	_all
log using 	"../log/A0_append_SSYK.log", replace 

{ // Appending year of occupation data (2001 to 2015)
*===============================================================================

*Open 2001 occupation data 
use 		"$rawdatapath/occup_2001.dta", clear



*Appending 2002 to 2013 for indivuals present in displacement data
forvalues i = 2002(1)2013 {
	append using "$rawdatapath/occup_`i'.dta"
}


append using "$rawdatapath/lisa_ind_2014.dta", keep(lopnr year ssyk*)


preserve
	use lopnr year ssyk* using "$rawdatapath/lisa_ind_2015.dta", clear
	replace ssyk3_2012 = ssyk3_2012_j16 if ssyk3_2012=="" & ssyk3_2012_j16!=""
	replace ssyk4_2012 = ssyk4_2012_j16 if ssyk4_2012=="" & ssyk4_2012_j16!=""
	replace ssykar = ssykar_j16 if  ssykar=="" & ssykar_j16!=""
	replace ssykstatus = ssykstatus_j16 if ssykstatus=="" & ssykstatus_j16!=""
	tempfile temp
	save 	`temp'
restore

append using `temp', keep(lopnr year ssyk3_2012 ssyk4_2012 ssykar ssykstatus)
}
*


*Replace missing values from differently named variables
replace ssyk3 = ssyk3_2012 if inrange(year,2014,2015) & ssyk3==""
replace ssyk4 = ssyk4_2012 if inrange(year,2014,2015) & ssyk4==""

*Drop variables
drop ssyk*_2012

*Replace missing values
replace ssyk3 = "" if ssyk3=="***"
replace ssyk4 = "" if ssyk4=="****"
replace ssykar = "" if ssykar=="****"

*Rename variables
rename lopnr persid

*Save (uniqe at lopnr year)
compress
save "$datapath/A0_SSYK_01_15.dta",replace



log close
